package com.lanou3g.zjl.chating.home.friends;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/26.
 */
public class GroupInfoActivity extends BaseActivity {
    @Bind(R.id.breakOrLeave)
    Button breakOrLeave;//退出或者登陆按钮
    @Bind(R.id.groupNum)
    TextView groupNum;//群成员
    @Bind(R.id.changeGroupName)
    RelativeLayout changeGroupName;//修改群名称
    @Bind(R.id.showGroupBlackList)
    RelativeLayout showGroupBlackList;//群黑名单
    @Bind(R.id.toChangeGroupName)
    LinearLayout toChangeGroupName;
    @Bind(R.id.changedGruopName)//新群名称
            EditText changedGruopName;
    @Bind(R.id.isRefuse)
    AnimateCheckBox isRefuse;

    private EMGroup group;//群对象
    private String groupId;
    private Handler handler;

    //点击查看群成员跳转到群成员界面
    @OnClick(R.id.showGroupMembers)
    void showGroupMember() {
        Intent intent = new Intent(GroupInfoActivity.this, GroupMembersActivity.class);
        intent.putExtra("Id", groupId);
        startActivity(intent);
    }

    //点击查看黑名单跳转到黑名单界面
    @OnClick(R.id.showGroupBlackList)
    void showBlackList() {
        Intent intent = new Intent(GroupInfoActivity.this, GroupBlackListActivity.class);
        intent.putExtra("groupID", groupId);
        startActivity(intent);
    }

    //点击修改群名称显示群名称编辑
    @OnClick(R.id.changeGroupName)
    void editGroupName() {
        if (toChangeGroupName.isShown()) {
            toChangeGroupName.setVisibility(View.GONE);
        } else {
            toChangeGroupName.setVisibility(View.VISIBLE);
        }
    }

    //点击保存按钮修改群名称
    @OnClick(R.id.saveNewGroupName)
    void saveNewGroupName() {
        if (TextUtils.isEmpty(changedGruopName.getText().toString())) {
            Toast.makeText(GroupInfoActivity.this, "群名称不能为空", Toast.LENGTH_SHORT).show();
        } else {

            changeGroupName(changedGruopName.getText().toString());
        }
    }

    @OnClick(R.id.finishGroupInfo) void finishGroupInfo(){
        finish();
    }
    //解散或者退出群聊
    @OnClick(R.id.breakOrLeave)
    void breakOrLeaveGroup() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(GroupInfoActivity.this);
        dialog.setTitle("提示");
        if (!TextUtils.isEmpty(breakOrLeave.getText())) {
            if (breakOrLeave.getText().equals("解散群聊")) {
                dialog.setMessage("你确定要解散该群吗");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        deleteGorup();
                        breakOrLeave.setEnabled(false);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                dialog.create().show();

            } else {
                dialog.setMessage("你确定要退出该群吗");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        leaveGroup();
                        breakOrLeave.setEnabled(false);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                dialog.create().show();

            }

        }
    }

    @Override
    protected int getLayout() {
        return R.layout.group_info_activity;
    }

    @Override
    protected void initData() {

        isRefuse.setCircleColor(Color.GREEN);
        isRefuse.setOnCheckedChangeListener(new AnimateCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(View buttonView, boolean isChecked) {
                if (isRefuse.isChecked()) {
                    refuseNews();
                } else {
                    acceptNews();
        }
            }
        });
        groupId = getIntent().getStringExtra("groupInfo");
        //初始化群信息
        getGroupInfo(groupId);
        getAsynk();
    }

    //解散群聊
    private void deleteGorup() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMGroupManager.getInstance().exitAndDeleteGroup(groupId);
                    handler.sendEmptyMessage(5);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //退出群聊
    private void leaveGroup() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMGroupManager.getInstance().exitFromGroup(groupId);
                    handler.sendEmptyMessage(6);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //解除屏蔽群消息
    private void acceptNews() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMGroupManager.getInstance().unblockGroupMessage(groupId);
                    handler.sendEmptyMessage(3);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //屏蔽群消息
    private void refuseNews() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMGroupManager.getInstance().blockGroupMessage(groupId);
                    handler.sendEmptyMessage(4);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //修改群名称的方法
    private void changeGroupName(final String changedGroupName) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMGroupManager.getInstance().changeGroupName(groupId, changedGroupName);
                    handler.sendEmptyMessage(2);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //获取子线程发送的消息
    private void getAsynk() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case 1:
                        if (group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())) {
                            isRefuse.setEnabled(false);
                        }
                        //判断该群是否屏蔽群消息
                        if (group.isMsgBlocked()) {
                            isRefuse.setChecked(true);
                        } else {
                            isRefuse.setChecked(false);
                        }
                        //当群对象获取成功之后设置相应的值
                        initView();
                        break;
                    case 2:
                        Toast.makeText(GroupInfoActivity.this, "群名称修改成功", Toast.LENGTH_SHORT).show();
                        toChangeGroupName.setVisibility(View.GONE);
                        break;
                    case 3:
                        Toast.makeText(GroupInfoActivity.this, "接收群消息", Toast.LENGTH_SHORT).show();
                        break;
                    case 4:
                        Toast.makeText(GroupInfoActivity.this, "屏蔽群消息", Toast.LENGTH_SHORT).show();
                        break;
                    case 5:
                        Toast.makeText(GroupInfoActivity.this, "群已解散", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                    case 6:
                        Toast.makeText(GroupInfoActivity.this, "你已退出该群", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                }
                return false;
            }
        });
    }

    //根据群id去服务器得到该群的详细信息
    private void getGroupInfo(final String groupId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    group = EMGroupManager.getInstance().getGroupFromServer(groupId);
                    if (group != null) {
                        handler.sendEmptyMessage(1);
                    }
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    //将群信息设置到界面
    private void initView() {
        //先隐藏按钮
        breakOrLeave.setVisibility(View.INVISIBLE);
        if (group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())) {
            breakOrLeave.setText("解散群聊");

        } else {
            breakOrLeave.setText("退出群聊");
            changeGroupName.setVisibility(View.GONE);
            showGroupBlackList.setVisibility(View.GONE);
        }
        groupNum.setText(groupId);
        //判断是否群主后再显示按钮
        breakOrLeave.setVisibility(View.VISIBLE);
    }
}
